<template>
	<div class="system-menu-container layout-pd">
		<el-card shadow="hover">
			<div class="system-menu-search mb15">
				<el-button size="default" type="success" class="ml10" @click="onOpenAddMenu('add')">
					<el-icon>
						<ele-FolderAdd />
					</el-icon>
					新增菜单
				</el-button>
				<el-button size="default" type="warning" class="ml10" @click="refreshCache">
					<el-icon>
						<ele-Refresh />
					</el-icon>
					刷新缓存
				</el-button>
			</div>
			<el-table size="default" :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%"
				row-key="path" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">

				<el-table-column label="菜单名称" show-overflow-tooltip>
					<template #default="scope">
						<SvgIcon v-if="scope.row.meta.icon" :name="scope.row.meta.icon" />
						<span class="ml10">{{ scope.row.meta.title }}</span>
					</template>
				</el-table-column>

				<el-table-column prop="path" label="路由路径" show-overflow-tooltip></el-table-column>
				<el-table-column label="组件路径" show-overflow-tooltip>
					<template #default="scope">
						<span>{{ scope.row.component }}</span>
					</template>
				</el-table-column>

				<el-table-column label="权限资源" align="center" show-overflow-tooltip>
					<template #default="scope">
						<span>{{ scope.row.functionResourceCode || "-" }}</span>
					</template>
				</el-table-column>

				<el-table-column label="类型" show-overflow-tooltip width="80">
					<template #default="scope">
						<el-tag type="success" size="small">菜单</el-tag>
					</template>
				</el-table-column>


				<el-table-column label="状态" show-overflow-tooltip width="80">
					<template #default="scope">
						<el-tag type="success" size="small" v-if="scope.row.status == 1">启用</el-tag>
						<el-tag type="danger" size="small" v-if="scope.row.status != 1">禁用</el-tag>
					</template>
				</el-table-column>

				<el-table-column label="操作" show-overflow-tooltip width="140">
					<template #default="scope">
						<el-button v-if="scope.row.currentLevel == 1" size="small" text type="primary"
							@click="onOpenAddMenu('add')">新增</el-button>
						<el-button size="small" text type="primary"
							@click="onOpenEditMenu('edit', scope.row)">修改</el-button>
						<el-button size="small" text type="primary" v-if="scope.row.status == 1"
							@click="updateMenuById(scope.row.menuId, '0')">禁用</el-button>
						<el-button size="small" text type="primary" v-if="scope.row.status != 1"
							@click="updateMenuById(scope.row.menuId, '1')">启用</el-button>
					</template>
				</el-table-column>
			</el-table>

		</el-card>
		<MenuDialog ref="menuDialogRef" @refresh="getMenusAdminAll()" />
	</div>
</template>

<script setup lang="ts" name="systemMenu">

import { defineAsyncComponent, ref, onMounted, reactive } from 'vue';
import { ElMessage } from 'element-plus';

// 导入接口
import { useMenuApi } from "/@/api/menu";


// 引入组件
const MenuDialog = defineAsyncComponent(() => import('/@/views/system/menu/dialog.vue'));

const menuDialogRef = ref();

const state = reactive({
	tableData: {
		data: [] as any,
		loading: true,
	},
});


// 打开新增菜单弹窗
const onOpenAddMenu = (type: string) => {
	menuDialogRef.value.openDialog(type);
};

// 打开编辑菜单弹窗
const onOpenEditMenu = (type: string, row: any) => {
	menuDialogRef.value.openDialog(type, row);
};


const getMenusAdminAll = async () => {
	state.tableData.loading = true;
	const params = {};

	state.tableData.data = [];
	let res = await useMenuApi().getMenusAdminAll(params);
	res = backEndInit(res);


	state.tableData.data = res;
	state.tableData.loading = false;
}




const refreshCache = async () => {
	try {
		await useMenuApi().refreshCache();
		ElMessage.success("刷新成功！");
	} catch (error: any) {
		ElMessage.error(error);
	}
}


/**
 * 修改菜单的状态
 * @param menuId 
 * @param status 
 */
const updateMenuById = async (menuId: string, status: string) => {
	const params = {
		menuId: menuId,
		status: status
	};
	try {
		await useMenuApi().updateMenuById(params);
		await getMenusAdminAll();
		ElMessage.success("操作成功");
	} catch (error) {
		ElMessage.error("操作失败请稍后再试");
	}
}


/**
 * 初始化菜单
 * @param routes 
 */
const backEndInit = (routes: any) => {
	if (!routes) return;
	let routesTemp: any = []
	routes.map((item: any) => {
		item.path = item.menuUrl;
		item.name = item.menuCode;
		item.component = item.componentPath;


		if (item.metaData) {
			item.meta = JSON.parse(item.metaData);
			if (item.meta.redirect) {
				item.redirect = item.meta.redirect;
			}

		} else {
			item.meta = {
				isKeepAlive: true, // 是否缓存
				isAffix: false, // 是否固定
				icon: "",
				isHide: false,
				isIframe: false,
				isLink: "",
				redirect: "",
				roles: [],
				title: item.menuName
			}
		}

		if (item.child) {
			item.children = backEndInit(item.child);
		}
		routesTemp.push(item);
	});
	return routesTemp;
}



// 页面加载时
onMounted(() => {
	getMenusAdminAll();
});



</script>
